package org.jeecg.modules.knowledgeType.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.ColumnManagerSystemVO;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.constant.RedisConstant;
import org.jeecg.modules.columnmanager.vo.ColumnManagerVO;
import org.jeecg.modules.knowledge.entity.PhilKnowledge;
import org.jeecg.modules.knowledge.service.IPhilKnowledgeService;
import org.jeecg.modules.knowledgeType.service.IKnowledgeColumnService;
import org.jeecg.modules.knowledgeType.vo.KnowledgeColumnVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;

/**
 * TODO:        !!!!!!!!!!!!!!!!!! 由于框架内置的字典管理有缓存，调用此类下的增删改方法会出现不能更新缓存的问题，还未找到解决的办法。。。
 *
 * @author Zhang Hao yu
 * @date 2020/11/28 14:23
 */
@Api(tags = "知识库栏目管理")
@RestController
@RequestMapping("/knowledge/column")
@Slf4j
public class KnowledgeColumnController extends JeecgController<KnowledgeColumnVO, IKnowledgeColumnService> {

    @Autowired
    private IKnowledgeColumnService columnService;

    @Autowired
    private ISysBaseAPI baseAPI;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IPhilKnowledgeService philKnowledgeService;
    /**
     * @功能：分页查询栏目信息
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<?> queryPageList(
                            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
            Result<IPage<ColumnManagerVO>> result = new Result<>();
            Page<ColumnManagerVO> page = new Page<>(pageNo, pageSize);
            IPage<ColumnManagerVO> pageList = columnService.myPage(page);
            result.setSuccess(true);
            result.setResult(pageList);
            return result;
    }

    /**
     * TODO: 不分页获取信息
     *
     *
     * @author ZhangHaoyu
     * @return org.jeecg.common.api.vo.Result<?>
     */
    @AutoLog(value = "app获取所有栏目")
    @ApiOperation(value="app获取所有栏目", notes="app获取所有栏目")
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public Result<?> query() {

        List<ColumnManagerVO> list = columnService.queryNotPage();

        // 获取知识总数
        int count = philKnowledgeService.count();
        //获取时间
        LocalDate today = LocalDate.now();
        LocalDate monday = today.with(DayOfWeek.MONDAY);
        // 获取本周更新数
        int countWeek = philKnowledgeService.count(
                new LambdaQueryWrapper<PhilKnowledge>()
                .ge(PhilKnowledge::getCreateTime,monday));
        HashMap<String, Object> map = new HashMap<>(3);
        map.put("info",list);
        map.put("total",count);
        map.put("newCount", countWeek);
        return Result.OK(map);
    }

    /**
     * @功能：新增
     * @return
     */
    @AutoLog(value = "栏目管理-添加")
    @ApiOperation(value="栏目管理-添加", notes="栏目管理-添加")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<ColumnManagerVO> add(@RequestBody ColumnManagerVO columnManagerVO, HttpServletRequest request) {
        Result<ColumnManagerVO> result = new Result<>();
        columnManagerVO.setColumnImage(columnManagerVO.getColumnImage());
        try {
            columnManagerVO.setDictId("1315497341812633602");
            // 自己设置id
            Snowflake snowflake = IdUtil.createSnowflake(1,1);
            String id = snowflake.nextIdStr();
            columnManagerVO.setId(id);
            ColumnManagerSystemVO columnManagerSystemVO = new ColumnManagerSystemVO();
            BeanUtils.copyProperties(columnManagerVO,columnManagerSystemVO);
            baseAPI.saveColumnManagerItem(columnManagerSystemVO,request);
            // 更新redis
            redisUtil.set(RedisConstant.ARTICLE_PUBLISH+id,new HashMap<String, Object>());
//            redisUtil.del(RedisConstant.ALL_COLUMN_INFO);
            result.success("保存成功！");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * @功能：修改
     * @return
     */
    @AutoLog(value = "栏目管理-修改")
    @ApiOperation(value="栏目管理-修改", notes="栏目管理-修改")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result<ColumnManagerVO> update(@RequestBody ColumnManagerVO columnManagerVO) {
        columnManagerVO.setColumnImage(columnManagerVO.getColumnImage());
        Result<ColumnManagerVO> result = new Result<>();
        try {
            columnManagerVO.setDictId("1315497341812633602");
            ColumnManagerSystemVO columnManagerSystemVO = new ColumnManagerSystemVO();
            BeanUtils.copyProperties(columnManagerVO,columnManagerSystemVO);
            baseAPI.updateColumnManagerItem(columnManagerSystemVO);
//            redisUtil.del(RedisConstant.ALL_COLUMN_INFO);
            result.success("修改成功！");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * @功能：删除
     * @return
     */
    @AutoLog(value = "栏目管理-删除")
    @ApiOperation(value="栏目管理-删除", notes="栏目管理-删除")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<ColumnManagerVO> delete(@RequestParam(name = "id") String id) {
        Result<ColumnManagerVO> result = new Result<>();
        try {
            columnService.myDelete(id);
           // 更新redis
           redisUtil.del(RedisConstant.ARTICLE_PUBLISH+id);
            result.success("删除成功！");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("操作失败");
        }
        return result;
    }

}